package com.isunland.managesystem.common;

import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.MyStringUtil;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class DefaultAsyncHttpClient {

  @SuppressWarnings("unused") private static final String TAG = "DefaultAsyncHttpClient";

  private static AsyncHttpClient client = init();

  private static AsyncHttpClient init() {
    client = new AsyncHttpClient();
    client.setTimeout(100000);

    client.setEnableRedirects(true, true);
    return client;
  }

  // 静态使用AsyncHttpClient.
  public static void get(String url, RequestParams params,
      AsyncHttpResponseHandler responseHandler) {
    client.addHeader("Cookie", RequestManager.getCookie());

    client.get(ApiConst.getAbsoluteUrl(url), params, responseHandler);
  }

  public static void post(String url, RequestParams params,
      AsyncHttpResponseHandler responseHandler) {
    //每次调用该方法前,刷新cookie.
    client.addHeader("Cookie", RequestManager.getCookie());
    LogUtil.i("url=", ApiConst.getAbsoluteUrl(url));
    LogUtil.d("params=", params);
    LogUtil.i("postman=", MyStringUtil.paramsToPostman(params));
    LogUtil.i("paramsJson=", MyStringUtil.toJson(params));
    client.post(ApiConst.getAbsoluteUrl(url), params, responseHandler);
  }

  /**
   * 取消请求
   */
  @SuppressWarnings("unused") public static void cancelAllRequest() {
    client.cancelAllRequests(true);
  }
}
